/***************************************************************************
 *   Copyright (C) 2008 by Rysiek KrÃÂ³l,,,   *
 *   rysiek@Babylon2   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#ifndef IMAGELOADER_H
#define IMAGELOADER_H

#include <QThread>
#include <QWaitCondition>
#include <QMutex>
#include <QImage>
#include <QSize>

#include "property.h"

class PictureListWidgetItem;
class PictureManager;
class QImage;

/**
	@author Rysiek Krol <rysiek.krol@gmail.com>
*/
class ImageLoader : public QThread
{
    Q_OBJECT
public:
    enum Action {GetNext=1, GetPrevious=2, GetIndex=3, GetCurrent=4, GetNextAndReload=5, Turn90=6, Turn180=7, Turn270=8, GetFirstSlide=9 ,GetNextSlide=10, GetPreviousSlide=11};
    ImageLoader( PictureManager *manager);
    ~ImageLoader();
    void setCurrentIndex(int theValue);
    int getCurrentIndex() const;
    void setCurrentAction(const Action& theValue);
    Action getCurrentAction() const;
    void setGoToSleepFalse();
    QList< ImageLoader :: Action > getActionList() const;
    bool getIsCurrentChanged() const;
    void setNextSlide(const QFileInfo& theValue);
    void setCurrentSlide(const QFileInfo& theValue);
    void setPreviousSlide(const QFileInfo& theValue);
    QFileInfo getCurrentSlide() const;
    void setCurrentChangedFalse ();
    QWaitCondition newActionCondition;
    QMutex waitMutex;
    
protected:
    void run();

private:
    QFileInfo nextSlide;
    QFileInfo currentSlide;
    QFileInfo previousSlide;
    QMutex slideMutex;
    Action currentAction;
    PictureManager *manager;
    QImage nextImage;
    QImage currentImage;
    QImage previousImage;
    QSize size;
    int currentIndex;
    bool goToSleep;
    QList<ImageLoader::Action> actionList;
    bool isCurrentChanged;

    void sendIndexedImage();
    void sendFirstSlide();
    void sendNextImage();
    void sendNextSlide();
    void sendNextAndReload();
    void sendPreviousImage();
    void sendProviousSlide();
    void sendCurrentImage();
    void reloadImages();
    QImage getScaledImage(QImage image);
    void rotateAndSendImage(int degree);

private slots:
    void propertyChangedSlot(Property::PropertyType typ);
};

#endif
